package com.ws.controller;

import com.ws.common.PaasPage;
import com.ws.common.PaasResponse;
import com.ws.entity.User;
import com.ws.response.UserListResponse;
import com.ws.response.UserPageResponse;
import com.ws.response.UserResponse;
import com.ws.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * @author ：com.ws
 * @date ：Created in 2023/10/22 16:03
 * @description：TODO
 * @modified By：
 * @version: 1.0.0
 */
@Controller
@RefreshScope
@CrossOrigin
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 员工分页查询接口
     * @param pageable
     * @param name
     * @param gender
     * @param departmentId
     * @return
     */
    @GetMapping("")
    public ResponseEntity<UserPageResponse> getUserPage(
            @PageableDefault(page = 1, direction = Sort.Direction.DESC) Pageable pageable,
            @RequestParam(value="name",required=false) String name,
            @RequestParam(value="gender",required=false) String gender,
            @RequestParam(value="departmentId",required=false) Long departmentId,
            @RequestParam(value="statusCd",required=false) String statusCd
    ){
        PaasPage<User> userPaasPage = userService.getUserPage(pageable,name,gender,departmentId,statusCd);
        return ResponseEntity.ok(new UserPageResponse(userPaasPage));
    }

    /**
     * 员工查询接口
     * @param name
     * @param gender
     * @param departmentId
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<UserListResponse> getUserList(
            @RequestParam(value="name",required=false) String name,
            @RequestParam(value="gender",required=false) String gender,
            @RequestParam(value="departmentId",required=false) Long departmentId,
            @RequestParam(value="statusCd",required=false) String statusCd
    ){
        List<User> userList = userService.getUserList(name,gender,departmentId,statusCd);
        return ResponseEntity.ok(new UserListResponse(userList));
    }

    /**
     * 员工新增接口
     * @param user
     * @return
     */
    @PostMapping("")
    public ResponseEntity<UserResponse> saveUser(@RequestBody @Valid User user){
        User user1 = userService.saveUser(user);
        return ResponseEntity.ok(new UserResponse(user1));
    }

    /**
     * 查询对应员工的基本信息
     * @param userId
     * @return
     */
    @GetMapping("/{userId}/basicInfo")
    public ResponseEntity<UserResponse> getUserBasicInfo(@PathVariable Long userId){
        User user = userService.getUserBasicInfo(userId);
        return ResponseEntity.ok(new UserResponse(user));
    }

    /**
     * 删除对应的员工
     * @param userId
     * @return
     */
    @PostMapping("/d/{userId}")
    public ResponseEntity delUser(@PathVariable Long userId)  {
        userService.delUser(userId);
        return ResponseEntity.ok(new PaasResponse());
    }

    /**
     * 修改对应的员工
     * @param userId
     * @return
     */
    @PostMapping("/u/{userId}")
    public ResponseEntity<UserResponse> updateUser(@PathVariable Long userId,@RequestBody User user)  {
        User user1 = userService.updateUser(userId,user);
        return ResponseEntity.ok(new UserResponse(user1));
    }


}
